﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace eLearning.Pages.PanelUtilities
{    
    public class ButtonMove : Button//Control
    {
        private Panel parent;
        private Panel superParent;
        bool mouseDown = false;
        bool moveable = false;

        Point newCursor;
        Point offset;
        Point oldCursor;
        Point bottomDown;

        public ButtonMove()
        {
            this.Loaded += new RoutedEventHandler(AttributeMove_Loaded);
            this.Click += new RoutedEventHandler(ButtonMove_Click);
            this.Content = moveable ? "M" : "D";
        }

        Thickness myThickness = new Thickness();
        void AttributeMove_Loaded(object sender, RoutedEventArgs e)
        {
            parent = ButtonEvent.FindParent<Panel>(this);
            if (parent == null) return;
            superParent = ButtonEvent.FindParent<Panel>(parent);

            if (superParent == null) return;

            parent.MouseLeftButtonDown += new MouseButtonEventHandler(parent_MouseLeftButtonDown);
            parent.MouseLeftButtonUp += new MouseButtonEventHandler(parent_MouseLeftButtonUp);
            parent.MouseMove += new MouseEventHandler(parent_MouseMove);
            parent.MouseLeave += new MouseEventHandler(parent_MouseLeave);

            myThickness = parent.Margin;
        }

        void ButtonMove_Click(object sender, RoutedEventArgs e) { moveable = !moveable; this.Content = moveable ? "M" : "D"; }

        void parent_MouseMove(object sender, MouseEventArgs e)
        {
            if (parent == null || superParent == null) return;
            Point p = e.GetPosition(parent);
            newCursor = e.GetPosition(superParent);

            offset.X = 2 * (newCursor.X - oldCursor.X);
            offset.Y = 2 * (newCursor.Y - oldCursor.Y);

            bottomDown.X = newCursor.X + parent.ActualWidth - p.X;
            bottomDown.Y = newCursor.Y + parent.ActualHeight - p.Y;

            if (mouseDown == true)
            {
                parent.Margin = new Thickness(
                      parent.Margin.Left + (p.X > newCursor.X ? 1 : (bottomDown.X > superParent.ActualWidth  ? -1 : offset.X))
                    , parent.Margin.Top  + (p.Y > newCursor.Y ? 1 : (bottomDown.Y > superParent.ActualHeight ? -1 : offset.Y))
                    , myThickness.Right
                    , myThickness.Bottom
                );
            }
            oldCursor = newCursor;
        }

        void parent_MouseLeave(object sender, MouseEventArgs e) { mouseDown = false; }
        void parent_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {mouseDown = false;}
        void parent_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {if (!moveable) return; oldCursor = e.GetPosition(superParent); mouseDown = true;}
    }
}
